<?php
class RoleAction extends CommonAction {

	public function index()
	{
		$this->assign('__JSLIB__',__ROOT__ .'/'.APP_PATH.'/public/js/');
		$this->display();
	}
	public function roledata(){
		$method = $_POST;
		$n = new NodeModel();
		$accessObj = D('access');
		$map['role_id'] = $method['roleid'];
		foreach($accessObj->where($map)->select() as $v){
			$access[$v['node_id']] = $v;
		}
		foreach($n->where('pid = 0 and level =1 and status=1')->select() as $v){
			if($access[$v['id']]) $v['checked'] = true;
			$rabc[$v['id']] = $v;
			$ids[] = $v['id'];
		}
		ksort($rabc);
		$map = array();
		$map['pid'] = array('in',$ids);
		$map['level'] = 2;
		$map['status'] = 1;
		$ids = array();
		foreach($n->where($map)->select() as $v){
			if($access[$v['id']]) $v['checked'] = true;
			$group[$v['pid']][$v['id']] = $v;
			$ids[] = $v['id'];
		}
		ksort($group);
		$map = array();
		$map['pid'] = array('in',$ids);
		$map['level'] = 3;
		$map['status'] = 1;
		foreach($n->where($map)->select() as $v){
			if($access[$v['id']]) $v['checked'] = true;
			$itemcheckbox[$v['pid']][$v['id']] = $v;
		}
		ksort($itemcheckbox);
		$arr = array('rabc'=>$rabc,'group'=>$group,'itemcheckbox'=>$itemcheckbox);
		echo json_encode($arr);
	}
	public function data()
	{
		$r = new RoleModel();
		$method = $_POST;
		switch($_POST['do'])
		{
			case 'roleid':
				R('User','data');
				break;
			case 'grouprole':
				$grouprole = $r->field('name,id')->select();
				$grouprole[] = array('name'=>'无上级组','id'=>0);
				//sort($grouprole);
				echo json_encode($grouprole);
				break;
			case 'grouproleid':
				$map['id'] = $_POST['where'];
				echo json_encode($r->where($map)->find());
				break;
			default:
				echo $r->getlist('*',$_POST['page'],$_POST['limit']);
			break;
		}

	}
	public function add() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$roleObj = new RoleModel();
			$data['ename'] = $method['ename'];
			$data['name'] = $method['name'];
			$data['pid'] = $method['pid'];
			$data['remark'] = $method['remark'];
			$data['status'] = $method['status'];
			$data['create_time'] = time();
			if($roleObj->add($data)){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}
	public function edit() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$roleObj = new RoleModel();
			$roleObj->id = $method['id'];
			$roleObj->ename = $method['ename'];
			$roleObj->name = $method['name'];
			$roleObj->pid = $method['pid'];
			$roleObj->remark = $method['remark'];
			$roleObj->status = $method['status'];
			if($roleObj->save()){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}
	public function delete() {
		if($this->isPost())
		{
			$method = json_decode(file_get_contents("php://input"))->list;
			$data = array();
			$roleObj = new RoleModel();
			$map['id'] = $method;
			if($roleObj->where($map)->delete()){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}
	public function setrole() {
		if($this->isPost())
		{
			$method = $_POST;
			$nodeObj = D('node');
			$accessObj = D('access');
			$method = $_POST;
			$data = array();
			$map['role_id'] = $method['rid'];
			$accessObj->where($map)->delete();
			$nodemap['id'] = array('in',$method['roleid']);
			foreach($nodeObj->where($nodemap)->select() as $v){
				$data = array();
				$data['role_id'] = $method['rid'];
				$data['node_id'] = $v['id'];
				$data['level'] = $v['level'];
				$data['pid'] = $v['pid'];
				$data['node_id'] = $v['id'];
				$accessObj->add($data);
			}
			$this->success('设置成功');
		}
	}

}